<template>
  <van-button
    v-if="value"
    class="follow-btn"
    round
    size="small"
    @click="onFollow"
    :loading="loading"
    >已关注</van-button
  >
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="loading"
    @click="onFollow"
    >关注</van-button
  >
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  components: {

  },
  props: {
    value: {
      type: Boolean,
      require: true
    },
    userId: {
      type: [Number, String, Object],
      require: true
    }
  },
  data () {
    return {
      article: {},
      loading: false
    }
  },
  created () {

  },
  methods: {
    // 关注/取消关注事件
    async onFollow () {
      this.loading = true // 加载中打开
      try {
        if (this.value) {
        // 已关注 点击取消关注
          await deleteFollow(this.userId)
        } else {
        // 没关注 点击关注
          await addFollow(this.userId)
        }
        // 修改按钮 显示 状态
        this.$emit('input', !this.value)
      } catch (err) {
        let message = '操作失败，请重试！'
        if (err.response && err.response.status === 400) {
          message = '用户不能关注自己哦~~'
        }
        this.$toast(message)
      }
      this.loading = false // 加载中关闭
    }
  }
}
</script>

<style lang="less" scoped>

</style>
